//
//  ViewController.m
//  GDMap
//
//  Created by lanou3g on 15/8/24.
//  Copyright (c) 2015年 大展宏图. All rights reserved.
//

#define TileOverlayViewControllerCoordinate CLLocationCoordinate2DMake(39.910695, 116.372830)


#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <MAMapKit/MAMapKit.h>
@interface ViewController ()< MAMapViewDelegate,MKMapViewDelegate >
@property (nonatomic, strong)MAMapView *mapView;
@property (nonatomic, strong)CLLocationManager *locationManager;
@property (nonatomic, strong) MAPointAnnotation *pointAnnotation;
@property (nonatomic, strong)CLLocation *location;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    
     [MAMapServices sharedServices].apiKey = @"76fbd24c3a2deb151426429dd94e1951";
    self.mapView = [[MAMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.mapView.delegate = self;
    [self.mapView setShowsUserLocation:YES];
    // 卫星定位
//    [self.mapView setMapType:MAMapTypeSatellite];
    //优化定位
    [self.mapView setMapType:MAMapTypeStandard];
    // 显示实时交通路况的代码
//    _mapView.showTraffic = YES ;
    
    [self.view addSubview:_mapView];
    // 大头针
   self.pointAnnotation = [[MAPointAnnotation alloc] init];
    _pointAnnotation.coordinate = CLLocationCoordinate2DMake(40.029, 116.337);
    _pointAnnotation.title = @"金五星商厦";
    _pointAnnotation.subtitle = @"毛纺路26号南甲";
    [_mapView addAnnotation:_pointAnnotation];
    
    [self.locationManager requestWhenInUseAuthorization];
    
    // 地图上显示图片覆盖物
//    self.mapView.centerCoordinate   = TileOverlayViewControllerCoordinate;
//    self.mapView.zoomLevel          = 19;
//    
//    [self.mapView addOverlay: [self constructTileOverlayWithFloor:2]];
  /*
    MACoordinateBounds coordinateBounds = MACoordinateBoundsMake(CLLocationCoordinate2DMake
                                                                 (39.939577, 116.388331),CLLocationCoordinate2DMake(39.935029, 116.384377));
    
    MAGroundOverlay *groundOverlay = [MAGroundOverlay groundOverlayWithBounds:coordinateBounds icon:[UIImage imageNamed:@"GWF"]];
    
    [_mapView addOverlay:groundOverlay];
    _mapView.visibleMapRect = groundOverlay.boundingMapRect;
    
    */
    
}

/*
//覆盖物
- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MAGroundOverlay class]])
    {
        MAGroundOverlayView *groundOverlayView = [[MAGroundOverlayView alloc]
                                                  initWithGroundOverlay:overlay];
        
        return groundOverlayView;
    }
    return nil;
}
*/

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}



- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= YES;       //设置气泡可以弹出，默认为NO
        annotationView.animatesDrop = YES;        //设置标注动画显示，默认为NO
        annotationView.draggable = YES;        //设置标注可以拖动，默认为NO
        annotationView.pinColor = MAPinAnnotationColorPurple;
        return annotationView;
    }
    return nil;
}

/*
// 地图上显示图片覆盖物
- (MATileOverlay *)constructTileOverlayWithFloor:(NSInteger)floor
{
    //
    // 构建tileOverlay的URL模版.
    NSString *URLTemplate = [NSString stringWithFormat: @"http://sdkdemo.amap.com:8080/tileserver/Tile?x={x}&y={y}&z={z}&f=%ld", (long)floor];
    
    MATileOverlay *tileOverlay = [[MATileOverlay alloc] initWithURLTemplate:URLTemplate];
    
    tileOverlay.minimumZ = 18; //设置可见最小Zoom值
    tileOverlay.maximumZ = 20; //设置可见最大Zoom值
    
    tileOverlay.boundingMapRect = MAMapRectForCoordinateRegion(MACoordinateRegionMakeWithDistance(TileOverlayViewControllerCoordinate, 200, 200)); //设置可渲染区域
    
    return tileOverlay;
}
*/
//  实现MAMapViewDelegate的mapView:viewForOverlay:函数，在瓦片显示在地图View上
//- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id <MAOverlay>)overlay
//{
//    if ([overlay isKindOfClass:[MATileOverlay class]])
//    {
//        MATileOverlayView *tileOverlayView = [[MATileOverlayView alloc] initWithTileOverlay:overlay];
//        
//        return tileOverlayView;
//    }
//    
//    return nil;
//}


- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks lastObject];
        
        userLocation.title = placemark.name;
        userLocation.subtitle = @"金五星商厦";
//        self.pointAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude ,userLocation.coordinate.longitude );
    }];
    
    
}


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState  {
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    if (newState == MKAnnotationViewDragStateEnding)  {

        
        CLLocation *location = [[CLLocation alloc] initWithLatitude:[view.annotation coordinate].latitude longitude:[view.annotation coordinate].longitude];
        [geocoder reverseGeocodeLocation:location  completionHandler:^(NSArray *placemarks, NSError *error) {
            CLPlacemark *placeMark = [placemarks lastObject];
            _pointAnnotation.title = placeMark.name;
            _pointAnnotation.subtitle = placeMark.subLocality;
        }];
    }
    
    
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
